<template>
  <div>
    <span>
      <h2>巡检计划</h2>
    </span>
    <el-button type="primary" size="mini" @click="showAddPlan()">新增计划</el-button>
    <el-button size="mini" type="primary" @click="list">查询</el-button>
    <el-button size="mini" type="info" @click="clear">清空</el-button>

    <el-form :inline="true" :model="plan1">
      <el-row>
        <el-col :span="4">
          <el-form-item label="巡检编号">
            <el-input v-model="plan1.planno" placeholder="请输入编号" size="mini" style="width: 100%;"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="巡检类型">
            <el-select v-model="plan1.pattype" placeholder="请选择巡检类型" size="mini" style="width: 100%;">
              <el-option label="常规巡检" value="0"></el-option>
              <el-option label="紧急巡检" value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="巡检周期">
            <el-select v-model="plan1.patcycle" placeholder="请选择巡检周期" size="mini" style="width: 100%;">
              <el-option label="天" value="0"></el-option>
              <el-option label="周" value="1"></el-option>
              <el-option label="月" value="2"></el-option>
              <el-option label="季" value="3"></el-option>
              <el-option label="半年" value="4"></el-option>
              <el-option label="年" value="5"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="开始日期">
            <el-date-picker v-model="plan1.startdate" type="date" placeholder="选择日期" size="mini" style="width: 100%;">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="结束日期">
            <el-date-picker v-model="plan1.enddate" type="date" placeholder="选择日期" size="mini" style="width: 100%;">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="4">
          <el-form-item>
            <div class="a">
              <el-button size="mini" type="primary" @click="list">查询</el-button>
              <el-button size="mini" type="info" @click="clear">清空</el-button>
            </div>
          </el-form-item>
        </el-col> -->
      </el-row>
    </el-form>

    <el-table :data="planList" border style="width: 100%">
      <el-table-column prop="id" label="序号" width="80px"></el-table-column>
      <el-table-column prop="planno" label="计划编号"></el-table-column>
      <el-table-column prop="planname" label="计划名称"></el-table-column>
      <el-table-column label="巡检类型">
        <template slot-scope="scope">
          <span>{{ scope.row.pattype == 0 ? "常规巡检" : "紧急巡检" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="巡检周期" width="80">
        <template slot-scope="scope">
          <span>{{
            scope.row.patcycle == 0
              ? "天"
              : scope.row.patcycle == 1
                ? "周"
                : scope.row.patcycle == 2
                  ? "月"
                  : scope.row.patcycle == 3
                    ? "季"
                    : scope.row.patcycle == 4
                      ? "半年"
                      : "年"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="startdate" label="开始日期"></el-table-column>
      <el-table-column prop="enddate" label="结束日期"></el-table-column>
      <el-table-column label="巡检点">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="showPoint(scope.row)" round plain>查看巡检点</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="showTaskDialog(scope.row)">任务分配</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->

    <!-- 分页开始 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageInfo.pageNum" :page-sizes="[5, 10, 20]" :page-size="pageInfo.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageInfo.total">
    </el-pagination>
    <!-- 分页结束 -->

    <!-- 添加表单开始 -->
    <el-dialog title="添加计划" :visible.sync="dialogVisibleAddPlan" width="30%">
      <el-form label-width="150px" :model="plan" :rules="rules" ref="addPlanForm">
        <el-form-item label="计划编号" prop="planno">
          <el-input v-model="plan.planno"></el-input>
        </el-form-item>
        <el-form-item label="计划名称" prop="planname">
          <el-input v-model="plan.planname"></el-input>
        </el-form-item>
        <el-form-item label="巡检类型" prop="pattype">
          <!-- 巡检类型下拉菜单 -->
          <el-select v-model="plan.pattype" placeholder="请选择巡检类型">
            <el-option label="常规巡检" value="0"></el-option>
            <el-option label="紧急巡检" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="巡检周期" prop="patcycle">
          <!-- 巡检周期下拉菜单 -->
          <el-select v-model="plan.patcycle" placeholder="请选择巡检周期">
            <el-option label="天" value="0"></el-option>
            <el-option label="周" value="1"></el-option>
            <el-option label="月" value="2"></el-option>
            <el-option label="季" value="3"></el-option>
            <el-option label="半年" value="4"></el-option>
            <el-option label="年" value="5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始日期" prop="startdate">
          <el-date-picker v-model="plan.startdate" type="date" placeholder="选择开始日期" style="width: 100%"></el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="enddate">
          <el-date-picker v-model="plan.enddate" type="date" placeholder="选择结束日期" style="width: 100%"></el-date-picker>
        </el-form-item>
        <el-form-item label="巡检地点" prop="pointIdList">
          <el-select v-model="plan.pointIdList" multiple collapse-tags>
            <el-option v-for="point in patrolPointList" :key="point.id" :value="point.id" :label="point.pointname"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleAddPlan = false">取 消</el-button>
        <el-button type="primary" @click="addPlan()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加表单结束 -->

    <el-dialog title="任务分配" :visible.sync="taskDialog">
      <el-button type="success" size="mini" @click="taskFPList.push({})">添加</el-button>
      <el-table :data="taskFPList">
        <el-table-column label="巡检人员">
          <template slot-scope="{row}">
            <el-select v-model="row.executor">
              <el-option v-for="user in userList" :key="user.id" :value="user.username" :label="user.username">
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="巡检地点">
          <template slot-scope="{row}">
            <el-select v-model="row.pointIdList" multiple collapse-tags>
              <el-option v-for="point in pointList" :key="point.id" :value="point.id" :label="point.pointname">
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="danger" @click="delTaskFPList(scope)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="taskDialog = false">取 消</el-button>
        <el-button type="primary" @click="addTask()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="巡检点信息" :visible.sync="pointDialog">
      <el-table :data="taskPointList">
        <el-table-column prop="pointno" label="巡检点编号" width="180">
        </el-table-column>
        <el-table-column prop="pointname" label="巡检点名称" width="180">
        </el-table-column>
        <el-table-column prop="address" label="巡检点地址">
        </el-table-column>
        <el-table-column prop="remark" label="备注">
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="pointDialog = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      rules: {
        planno: [
          { required: true, message: '请输入计划编号', trigger: 'blur' },
          { validator: this.validatePlanno, trigger: 'blur' }
        ],
        planname: [
          { required: true, message: '请输入计划名称', trigger: 'blur' }
        ],
        pattype: [
          { required: true, message: '请选择巡检类型', trigger: 'change' }
        ],
        patcycle: [
          { required: true, message: '请选择巡检周期', trigger: 'change' }
        ],
        startdate: [
          { required: true, message: '请选择开始日期', trigger: 'change' }
        ],
        enddate: [
          { required: true, message: '请选择结束日期', trigger: 'change' }
        ],
        pointIdList: [
          { required: true, message: '请选择巡检地点', trigger: 'blur' }
        ]
      },
      taskPointList: [],
      //全部巡检点
      patrolPointList: [],
      planId: "",
      //计划巡检点
      pointList: [],
      userList: [],
      taskFPList: [],
      dialogVisibleAddPlan: false,
      plan: {},
      plan1: {
        planno: "",
        pattype: "",
        patcycle: "",
        startdate: "",
        enddate: "",
      },
      planList: [

      ],
      pageInfo: {
        pageNum: 1,
        pageSize: 5,
      },
      taskDialog: false,
      pointDialog: false
    };
  },
  methods: {
    getAllPlans() {
      this.$axios.get("/patrolplan/findAll", { params: { pageNum: 1, pageSize: 10000 } })
        .then((result) => {
          let vo = result.data;
          if (vo.code == 200) {
            this.allPlanList = vo.data.list;
          } else {
            this.$message.error(vo.message || "获取计划数据失败");
          }
        })
        .catch((err) => {
          console.log(err);
          this.$message.error("请求失败，请检查网络或参数");
        });
    },
    validatePlanno(rule, value, callback) {
      if (this.allPlanList.some(item => item.planno === value)) {
        callback(new Error('计划编号已存在'));
      } else {
        callback();
      }
    },
    showPoint(row) {
      this.$axios.get('/patrolPoint/plan/' + row.id)
        .then(res => {
          this.taskPointList = res.data.data
          this.pointDialog = true
        })
    },
    delTaskFPList(scope) {
      this.taskFPList.splice(scope.$index, 1);
    },
    addTask() {
      this.$axios.post('/patrolTask/add/' + this.planId, this.taskFPList)
        .then(res => {
          this.taskDialog = false
          this.$message.success('操作成功')
        })
    },
    showTaskDialog(row) {
      this.planId = row.id
      this.$axios.get('/user/roleid/3')
        .then(res => {
          this.userList = res.data.data
          this.$axios.get('/patrolPoint/plan/' + row.id)
            .then(res => {
              this.pointList = res.data.data
              this.$axios.get('/patrolTask/planId/' + row.id)
                .then(res => {
                  this.taskFPList = res.data.data
                })
            })
        })
      this.taskDialog = true
    },
    clear() {
      this.plan1 = {
        planno: "",
        pattype: "",
        patcycle: "",
        startdate: "",
        enddate: "",
      };
    },
    addPlan() {
      this.$refs.addPlanForm.validate((valid) => {
        if (valid) {
          this.$axios
            .post("/patrolplan/addPatrolPlan", this.plan)
            .then((result) => {
              let vo = result.data;
              this.$message(vo.message);
              this.dialogVisibleAddPlan = false;
              this.list();
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          this.$message.error('表单校验失败，请检查输入');
        }
      });
    },
    showAddPlan() {
      this.plan = {}
      this.getAllPlans();
      this.$axios.get('/patrolPoint/all')
        .then(res => {
          this.patrolPointList = res.data.data
        })
      this.dialogVisibleAddPlan = true;
    },
    handleCurrentChange(num) {
      this.pageInfo.pageNum = num;
      this.list();
    },
    handleSizeChange(size) {
      this.pageInfo.pageSize = size;
      this.list();
    },
    formatDate(date) {
      if (!date) return null;
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, "0");
      const day = date.getDate().toString().padStart(2, "0");
      const hours = date.getHours().toString().padStart(2, "0");
      const minutes = date.getMinutes().toString().padStart(2, "0");
      const seconds = date.getSeconds().toString().padStart(2, "0");
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    list() {
      const params = {
        pageNum: this.pageInfo.pageNum,
        pageSize: this.pageInfo.pageSize,
        planno: this.plan1.planno || null,
        pattype: this.plan1.pattype || null,
        patcycle: this.plan1.patcycle || null,
        startdate: this.formatDate(this.plan1.startdate),
        enddate: this.plan1.enddate ? this.formatDate(this.plan1.enddate) : this.formatDate(new Date()),
      };

      this.$axios
        .get("/patrolplan/findAll", { params })
        .then((result) => {
          let vo = result.data;
          if (vo.code == 200) {
            this.pageInfo.total = vo.data.total;
            this.planList = vo.data.list;
          } else {
            this.$message.error(vo.message || "查询失败");
          }
        })
        .catch((err) => {
          console.log(err);
          this.$message.error("请求失败，请检查网络或参数");
        });
    },
  },
  mounted() {
    this.list();
  },
};
</script>
<style>
.a {
  margin-top: 35px;
}
</style>